package com.wxzz.elearing.course.api.docent;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.*;
import com.wxzz.elearing.course.entity.*;
import com.wxzz.elearing.course.service.ICurriculumSectionTopicService;
import com.wxzz.elearing.course.service.ICurriculumService;
import com.wxzz.elearing.course.service.IWxUserCurriculumService;
import com.wxzz.elearing.course.vo.CurriculumVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/docent-curriculum")
@Api(tags = "课程接口(讲师端)")
public class DocentCurriculumController {

    @Autowired
    private ICurriculumService curriculumService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IWxUserCurriculumService wxUserCurriculumService;

    @ApiOperation(value = "课程列表(讲师端)", notes = "课程列表(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "listCurriculumByCategory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Curriculum>>> listCurriculumByCategory(HttpServletResponse response, @RequestHeader String token, @RequestBody CurriculumDto curriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        curriculumDto.setDocentId(jsonObject.getLong("docentId"));
        return curriculumService.listCurriculumByCategory(response,0, curriculumDto,0);
    }

    @ApiOperation(value = "查看参与学习人(讲师端)", notes = "查看参与学习人(讲师端)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listWxUserCurriculum", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserCurriculum>>> listWxUserCurriculum(@RequestHeader String token, @RequestBody UserCurriculumDto userCurriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        userCurriculumDto.setDocentId(jsonObject.getLong("docentId"));
        return wxUserCurriculumService.listWxUserCurriculum(userCurriculumDto);
    }

//    @ApiOperation(value = "查询课程详情(讲师端)", notes = "查询课程详情(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "selectCurriculumDetail/{curriculumId}", method = RequestMethod.POST)
//    public ReturnVO<CurriculumVo> selectCurriculumDetail(@ApiParam("课程id") @PathVariable Long curriculumId) {
//        return curriculumService.selectCurriculumDetail(curriculumId);
//    }
//
//    @ApiOperation(value = "查询课程章节测评列表(讲师端)", notes = "查询课程章节测评列表(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "listCurriculumSectionTopic/{curriculumSectionId}", method = RequestMethod.POST)
//    public ReturnVO<List<CurriculumSectionTopic>> listCurriculumSectionTopic(@ApiParam("课程章节id") @PathVariable Long curriculumSectionId) {
//        return curriculumService.listCurriculumSectionTopic(curriculumSectionId, 0);
//    }

    @ApiOperation(value = "添加课程(平台课程)(讲师端)", notes = "添加课程/企业申请平台课程(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "savePlatformCurriculum", method = RequestMethod.POST)
    public ReturnVO savePlatformCurriculum(@RequestHeader String token, @RequestBody Curriculum curriculum) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        curriculum.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        curriculum.setDocentId(jsonObject.getLong("docentId"));
        curriculum.setIsDocentSend(1);
        curriculum.setCurriculumType(0);
        return curriculumService.saveCurriculum(jsonObject.getLong("id"),curriculum);
    }


    @ApiOperation(value = "添加课程(企业内部课程)(讲师端)", notes = "添加课程/企业申请平台课程(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "saveEnterpriseCurriculum", method = RequestMethod.POST)
    public ReturnVO saveEnterpriseCurriculum(@RequestHeader String token, @RequestBody Curriculum curriculum) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        curriculum.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        curriculum.setDocentId(jsonObject.getLong("docentId"));
        curriculum.setCurriculumType(1);
        return curriculumService.saveCurriculum(jsonObject.getLong("id"),curriculum);
    }

    @ApiOperation(value = "上架课程(讲师端)", notes = "上架课程(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "updateStatus/{curriculumId}", method = RequestMethod.POST)
    public ReturnVO updateStatus(@RequestHeader String token, @PathVariable Long curriculumId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        Integer isDocent = jsonObject.getInteger("isDocent");
        //讲师端上架的课程需要审核
        return curriculumService.updateStatus(curriculumId, isDocent);
    }

    @ApiOperation(value = "企业上架课程到平台(讲师端)", notes = "企业上架课程到平台(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "updatePlatformStatus", method = RequestMethod.POST)
    public ReturnVO updatePlatformStatus(@Valid @RequestBody UpdatePlatformStatusDto updatePlatformStatusDto) {
        return curriculumService.updatePlatformStatus(updatePlatformStatusDto);
    }

    @ApiOperation(value = "审核修改课程日志列表(讲师端)", notes = "审核修改课程日志列表(讲师端)", httpMethod = "POST", tags = "讲师端")
    @RequestMapping(value = "listCurriculumCheckLogByDocentId", method = RequestMethod.POST)
    public ReturnVO listCurriculumCheckLogByDocentId(@RequestHeader String token,@RequestBody CurriculumCheckLogDto curriculumCheckLogDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        curriculumCheckLogDto.setDocentId(jsonObject.getLong("docentId"));
        return curriculumService.listCurriculumCheckLogByDocentId(curriculumCheckLogDto);
    }


//    @ApiOperation(value = "修改课程(讲师端)", notes = "修改课程(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "updateCurriculum", method = RequestMethod.POST)
//    public ReturnVO updateCurriculum(@RequestBody Curriculum curriculum) {
//        return curriculumService.updateCurriculum(curriculum);
//    }
//
//    @ApiOperation(value = "删除课程(讲师端)", notes = "删除课程(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "deleteCurriculum/{curriculumId}", method = RequestMethod.POST)
//    public ReturnVO deleteCurriculum(@PathVariable Long curriculumId) {
//        return curriculumService.deleteCurriculum(curriculumId);
//    }

//    @ApiOperation(value = "添加或修改课程章节(讲师端)", notes = "添加或修改课程章节(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "saveCurriculumSection", method = RequestMethod.POST)
//    public ReturnVO saveCurriculumSection(@RequestBody CurriculumSection curriculumSection) {
//        return curriculumService.saveCurriculumSection(curriculumSection);
//    }
//
//    @ApiOperation(value = "根据章节id查询详情(讲师端)", notes = "根据章节id查询详情", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "selectCurriculumSectionDetail/{curriculumSectionId}", method = RequestMethod.POST)
//    public ReturnVO<CurriculumSection> selectCurriculumSectionDetail(@PathVariable Long curriculumSectionId) {
//        return curriculumService.selectCurriculumSectionDetail(curriculumSectionId);
//    }
//
//    @ApiOperation(value = "新增章节时根据课程id展示单元(讲师端)", notes = "新增章节时根据课程id展示单元(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "listCurriculumUnit/{curriculumId}", method = RequestMethod.POST)
//    public ReturnVO<List<CurriculumUnit>> listCurriculumUnit(@PathVariable Long curriculumId) {
//        return curriculumService.listCurriculumUnit(curriculumId);
//    }
//
//    @ApiOperation(value = "新增课程单元(讲师端)", notes = "新增课程单元(讲师端)", httpMethod = "POST", tags = "讲师端")
//    @RequestMapping(value = "saveCurriculumUnit", method = RequestMethod.POST)
//    public ReturnVO saveCurriculumUnit(@RequestBody List<CurriculumUnit> curriculumUnit) {
//        return curriculumService.saveCurriculumUnit(curriculumUnit);
//    }
}
